import type {
  BlogFrameworkConfig,
  PlatformElementConfig,
  StyleConfigMap,
} from '../../interfaces'
import { HEADING_TAG } from '../base'
import { preprocessElementConfigs } from '../personalization'
import { COMMON_PAGE_ELEMENTS } from './common'

/**
 * 页面元素配置
 */
export const JUE_JIN_PAGE_ELEMENTS: Record<string, PlatformElementConfig> = {
  ...COMMON_PAGE_ELEMENTS,
  // 基础容器
  CONTENT_CONTAINER: {
    id: 'content-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `article`,
    inherit: false,
    styleVar: '--page-content-container',
  },
  INFO_CONTAINER: {
    id: 'meta-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 30,
    selector: `.author-info-block`,
    inherit: false,
    styleVar: '--page-info-container',
  },
  ARTICLE_CONTAINER: {
    id: 'article-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 55,
    selector: `article`,
    inherit: false,
    styleVar: '--page-article-container',
  },
  // 文章标题
  ARTICLE_TITLE: {
    id: 'article-title',
    category: 'title-container',
    categoryKey: 'category',
    priority: 40,
    selector: `article .article-title`,
    inherit: false,
    styleVar: '--page-article-title',
  },
  // 文章元数据
  ARTICLE_AUTHOR: {
    id: 'article-author',
    category: 'meta-container',
    categoryKey: 'category',
    priority: 45,
    selector: `.author-info-block .author-name a`,
    inherit: false,
    styleVar: '--page-article-author',
  },
  // 文章内容
  ARTICLE_WRAPPER: {
    id: 'article-wrapper',
    category: 'article-container',
    categoryKey: 'category',
    priority: 55,
    selector: 'article #article-root > div',
    inherit: false,
    styleVar: '--page-article-wrapper',
  },
  ARTICLE_CONTENT: {
    id: 'article-content',
    category: 'article-container',
    categoryKey: 'category',
    priority: 60,
    selector: 'article #article-root p',
    inherit: false,
    styleVar: '--page-article-content',
  },
  ARTICLE_HEADING: {
    id: 'article-heading',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: `article #article-root :is(${HEADING_TAG})`,
    inherit: false,
    styleVar: '--page-article-heading',
  },
  CONTENT_HR: {
    id: 'content-hr',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article-root hr',
    inherit: false,
    styleVar: '--page-content-hr',
  },
  CONTENT_PRE: {
    id: 'content-pre',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article-root pre',
    inherit: false,
    styleVar: '--page-content-pre',
  },
  CONTENT_CODE: {
    id: 'content-code',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article-root code',
    inherit: false,
    styleVar: '--page-content-code',
  },
  CONTENT_TABLE: {
    id: 'content-table',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article-root table',
    inherit: false,
    styleVar: '--page-content-table',
  },
  CONTENT_MARK: {
    id: 'content-mark',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article-root mark',
    inherit: false,
    styleVar: '--page-content-mark',
  },
  CONTENT_LINK: {
    id: 'content-link',
    category: 'article-content',
    categoryKey: 'category',
    priority: 62,
    selector: 'article #article-root a[href^="http"]',
    inherit: false,
    styleVar: '--page-content-link',
  },
  CONTENT_UL_LI: {
    id: 'content-ul-li',
    category: 'article-content',
    categoryKey: 'category',
    priority: 63,
    selector: 'article #article-root ul li',
    inherit: false,
    styleVar: '--page-content-li',
  },
  CONTENT_OL_LI: {
    id: 'content-ol-li',
    category: 'article-content',
    categoryKey: 'category',
    priority: 63,
    selector: 'article #article-root ol li',
    inherit: false,
    styleVar: '--page-content-ol-li',
  },
  CONTENT_BLOCK_QUOTE: {
    id: 'content-blockquote',
    category: 'article-content',
    categoryKey: 'category',
    priority: 64,
    selector: 'article #article-root blockquote',
    inherit: false,
    styleVar: '--page-content-blockquote',
  },
}
// 预处理配置
const PROCESSED_JUE_JIN_ELEMENTS = preprocessElementConfigs(JUE_JIN_PAGE_ELEMENTS)

export type JueJinElements = typeof JUE_JIN_PAGE_ELEMENTS
export type JueJinElementKey = keyof typeof JUE_JIN_PAGE_ELEMENTS
export type JueJinElementValue = typeof JUE_JIN_PAGE_ELEMENTS[JueJinElementKey]

/**
 * 框架配置类型
 */
export type JueJinFrameworkConfig = {
  [K in keyof BlogFrameworkConfig]: Partial<StyleConfigMap>
}

/**
 * 框架配置
 */
export const JUE_JIN_FRAMEWORK_CONFIG: JueJinFrameworkConfig = {
  GLOBAL: {
    shortcuts: [
      PROCESSED_JUE_JIN_ELEMENTS.SHORTCUTS,
    ],
    pageTheme: [
      PROCESSED_JUE_JIN_ELEMENTS.PAGE_THEME,
    ],
    loadingTheme: [
      PROCESSED_JUE_JIN_ELEMENTS.PAGE_LOADING,
    ],
    loadingPhrases: [
      PROCESSED_JUE_JIN_ELEMENTS.PAGE_LOADING,
    ],
    elementHide: [
      PROCESSED_JUE_JIN_ELEMENTS.PAGE_ELEMENTS,
    ],
  },
  WRAPPER: {
  },
  TITLE: {
  },
  META: {
  },
  CONTENT: {
  },
}
